package com.one.controller;

import com.one.dto.GridMemberDTO;
import com.one.dto.GridMemberLoginDTO;
import com.one.dto.GridMemberPageRequestDTO;
import com.one.dto.PageResponseDto;
import com.one.entity.GridMember;
import com.one.properties.JwtProperties;
import com.one.service.GridMemberService;
import com.one.utils.JwtUtil;
import com.one.vo.GridMemberLoginVO;
import com.one.vo.GridMemberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/gridMember")
@Api(tags = "网格员相关接口")
public class GridMemberController {

	@Autowired
	private GridMemberService gridMemberService;

	@Autowired
	private JwtProperties jwtProperties;
	
	/************************** nepm工程 **************************/
	@PostMapping("/listGridMemberByProvinceId")
	@ApiOperation(value = "根据省份ID、市区ID和网格员状态 查询网格员列表")
	public List<GridMember> listGridMemberByProvinceId(@RequestBody GridMember gridMember){
		log.info("根据省份ID、市区Id和网格员状态查询网格员列表: {}", gridMember);
		return gridMemberService.listGridMemberByProvinceId(gridMember);
	}
	
	/************************** nepm工程 **************************/
	
	/************************** nepg工程 **************************/
	@PostMapping("/getGridMemberByCodeByPass")
	@ApiOperation(value = "根据网格员编号和密码查询网格员信息，网格员登录")
	public GridMemberLoginVO getGridMemberByCodeByPass(@RequestBody GridMemberLoginDTO gridMemberLoginDTO){
		log.info("网格员登录请求: {}", gridMemberLoginDTO);
		GridMember gridMemberLogin = gridMemberService.getGridMemberByCodeByPass(gridMemberLoginDTO);
		if (gridMemberLogin == null) {return null; }// 登录失败，返回null
		//登录成功后，生成jwt令牌
		Map<String, Object> claims = new HashMap<>();
		claims.put("gmCode", gridMemberLogin.getGmCode());
		claims.put("gmName", gridMemberLogin.getGmName());
		String token = JwtUtil.createJWT(
				jwtProperties.getUserSecretKey(),
				jwtProperties.getUserTtl(),
				claims);
		// 封装返回结果(监督员信息和token令牌)
		GridMemberLoginVO gridMemberLoginVO = GridMemberLoginVO.builder()
				.gmId(gridMemberLogin.getGmId())
				.gmName(gridMemberLogin.getGmName())
				.gmCode(gridMemberLogin.getGmCode())
				.provinceId(gridMemberLogin.getProvinceId())
				.cityId(gridMemberLogin.getCityId())
				.tel(gridMemberLogin.getTel())
				.state(gridMemberLogin.getState())
				.remarks(gridMemberLogin.getRemarks())
				.token(token)
				.build();
		return gridMemberLoginVO;
	}
	/************************** nepg工程 **************************/

	/************************** nepm工程 **************************/
	@PostMapping("/addGridmember")
	@ApiOperation(value = "增加网格员")
	public int addGridMember(@RequestBody GridMemberDTO gridMemberDTO) {
		log.info("增加网格员请求：{}", gridMemberDTO);
		int result = gridMemberService.addGridmember(gridMemberDTO);
		return result;
	}

	@GetMapping("/deleteGridmember/{gmId}")
	@ApiOperation(value = "删除网格员")
	public int deleteGridmember(@PathVariable Integer gmId) {
		log.info("删除网格员请求：{}", gmId);
		int result = gridMemberService.deleteGridMember(gmId);
		return result;
	}

	@PostMapping("/updateGridmember")
	@ApiOperation(value = "更新网格员状态")
	public int updateGridmember(@RequestBody GridMember gridMember) {
		log.info("更新网格员状态请求：{}", gridMember);
		int result = gridMemberService.updateGridMember(gridMember);
		return result;
	}

	@PostMapping("/listGridmember")
	@ApiOperation(value = "分页查询网格员")
	public PageResponseDto<GridMemberVO> listGridMember(@RequestBody GridMemberPageRequestDTO gridMemberPageRequestDTO){
		log.info("网格员数据分页查询请求：{}",gridMemberPageRequestDTO);
		return gridMemberService.listGridMember(gridMemberPageRequestDTO);
	}
	
	/************************** nepm工程 **************************/
}











